package org.szgb.console.ht.electricity.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import org.szgb.console.ht.electricity.entity.HtElectricitySettlementBillItemModel;
import org.szgb.console.ht.electricity.mapper.HtElectricitySettlementBillItemModelMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class HtElectricitySettlementBillItemModelService {
    private final HtElectricitySettlementBillItemModelMapper htElectricitySettlementBillItemModelMapper;

    @Autowired
    public HtElectricitySettlementBillItemModelService(HtElectricitySettlementBillItemModelMapper htElectricitySettlementBillItemModelMapper){
        this.htElectricitySettlementBillItemModelMapper = htElectricitySettlementBillItemModelMapper;
    }

    @Transactional(readOnly = true)
    public HtElectricitySettlementBillItemModel get(String id) {
        return htElectricitySettlementBillItemModelMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<HtElectricitySettlementBillItemModel> findList(HtElectricitySettlementBillItemModel htElectricitySettlementBillItemModel) {
        if (htElectricitySettlementBillItemModel != null && htElectricitySettlementBillItemModel.getPageDto() != null) {
            Page<HtElectricitySettlementBillItemModel> page = new Page<>(htElectricitySettlementBillItemModel.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            htElectricitySettlementBillItemModel.setPage(page);
        }
        return htElectricitySettlementBillItemModelMapper.findList(htElectricitySettlementBillItemModel);
    }

    @Transactional(readOnly = true)
    public Integer getCount(HtElectricitySettlementBillItemModel htElectricitySettlementBillItemModel) {
        return htElectricitySettlementBillItemModelMapper.getCount(htElectricitySettlementBillItemModel);
    }

    @Transactional()
    public void save(HtElectricitySettlementBillItemModel htElectricitySettlementBillItemModel) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (htElectricitySettlementBillItemModel.getId() == null || htElectricitySettlementBillItemModel.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            htElectricitySettlementBillItemModel.setCreatedBy(htElectricitySettlementBillItemModel.getUpdatedBy());
            // 添加UUID
            htElectricitySettlementBillItemModel.setId(UUIDUtil.getUUID());
            // 执行插入操作
            htElectricitySettlementBillItemModelMapper.insert(htElectricitySettlementBillItemModel);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            htElectricitySettlementBillItemModelMapper.update(htElectricitySettlementBillItemModel);
        }
    }

    @Transactional
    public void delete(String ids) {
        htElectricitySettlementBillItemModelMapper.delete(ids.split(","));
    }
}
